﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using System.Windows.Controls.DataVisualization.Charting;
using System.ComponentModel;


namespace IndexMarkSilverlightClient.Views.Controls
{
    public partial class DistibutionIndexes : UserControl, INotifyPropertyChanged
    {
        private IndexMarkServiceReference.IndexMarkServiceClient client = new IndexMarkServiceClient();
        private int TermID;
        private ObservableCollection<IndexRow> indexes = new ObservableCollection<IndexRow>();
        private List<KeyValuePair<string, double>> IndexesMain = new List<KeyValuePair<string, double>>();
        private List<KeyValuePair<string, double>> IndexesLider = new List<KeyValuePair<string, double>>();
        private List<KeyValuePair<string, double>> IndexesHarmon = new List<KeyValuePair<string, double>>();


        public DistibutionIndexes(IndexStrategyType strategy = IndexStrategyType.PPS)
        {
            StrategyType = strategy;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            client.GetIndexes2Completed += client_GetIndexesCompleted;
            //var vm = (((((((this.Parent as Grid).Parent as Grid).Parent as ReportsMain).Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).ViewModel;            
            var vm = MainPage.ViewModel;
            vm.SelectTerm -= ViewModel_SelectTerm;
            vm.SelectTerm += new MainViewModel.SelectTermEventHandler(ViewModel_SelectTerm);
            vm.SelectTermInit();
            

            TypeCB.Visibility = System.Windows.Visibility.Collapsed;
            IndexesChart.Visibility = System.Windows.Visibility.Collapsed;
        }

        void client_GetIndexesCompleted(object sender, GetIndexes2CompletedEventArgs e)
        {
            indexes = e.Result;
            CalcIndexes();
        }



        private void CalcIndexes()
        {
            IndexesMain.Clear();
            IndexesLider.Clear();
            IndexesHarmon.Clear();

            int IntervalCount = Convert.ToInt32((TypeCB.SelectedItem as ComboBoxItem).Content);
            int Interval = 1000 / IntervalCount;

            for (int i = 0; i < IntervalCount; i++)
            {
                int BottomLimit = 0 + i * Interval;
                int TopLimit = BottomLimit + Interval;

                IndexesMain.Add(new KeyValuePair<string, double>(BottomLimit.ToString() + " - " + TopLimit.ToString(), 0));
                IndexesLider.Add(new KeyValuePair<string, double>(BottomLimit.ToString() + " - " + TopLimit.ToString(), 0));
                IndexesHarmon.Add(new KeyValuePair<string, double>(BottomLimit.ToString() + " - " + TopLimit.ToString(), 0));
            }

            foreach (IndexRow row in indexes)
            {
                for (int i = 0; i < IntervalCount; i++)
                {
                    int BottomLimit = 0 + i * Interval;
                    int TopLimit = BottomLimit + Interval;

                    if (row.Index >= BottomLimit && row.Index <= TopLimit)
                    {
                        IndexesMain[i] = new KeyValuePair<string,double>(IndexesMain[i].Key, IndexesMain[i].Value + 1);
                    }
                    if (row.IndexHarmony >= BottomLimit && row.IndexHarmony <= TopLimit)
                    {
                        IndexesHarmon[i] = new KeyValuePair<string, double>(IndexesMain[i].Key, IndexesMain[i].Value + 1);
                    }
                    if (row.IndexLider.Index >= BottomLimit && row.IndexLider.Index <= TopLimit)
                    {
                        IndexesLider[i] = new KeyValuePair<string, double>(IndexesMain[i].Key, IndexesMain[i].Value + 1);
                    }
                }
            }

            TypeCB.Visibility = System.Windows.Visibility.Visible;
            IndexesChart.Visibility = Visibility.Visible;
            
            DrawIndexes(IndexesMain, IndexesLider, IndexesHarmon);
        }

        void ViewModel_SelectTerm(object sender, Classes.SelectTermEventsArgs e)
        {
            TermID = e.SelectTerm.IDTerm;
            
            client.GetIndexes2Async(TermID, new ObservableCollection<int>(), new ObservableCollection<int>(), 
                new ObservableCollection<int>(),IndexListType.Compact,StrategyType);
            
            TypeCB.Visibility = System.Windows.Visibility.Collapsed;
            IndexesChart.Visibility = Visibility.Collapsed;

        }

        private void DrawIndexes(List<KeyValuePair<string, double>> IndexesMain, List<KeyValuePair<string, double>> IndexesLider, List<KeyValuePair<string, double>> IndexesHarmon)
        {
            (IndexesChart.Series[0] as ColumnSeries).ItemsSource = null; 
            (IndexesChart.Series[0] as ColumnSeries).ItemsSource = IndexesMain;

            (IndexesChart.Series[1] as ColumnSeries).ItemsSource = null; 
            (IndexesChart.Series[1] as ColumnSeries).ItemsSource = IndexesLider;
            
            (IndexesChart.Series[2] as ColumnSeries).ItemsSource = null; 
            (IndexesChart.Series[2] as ColumnSeries).ItemsSource = IndexesHarmon;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private IndexStrategyType StrategyType;

        private void TypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (indexes.Count != 0) CalcIndexes();
        }

    }
}
